package com.xuecheng.search.listener;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.search.service.SearchService;
import com.xuecheng.web.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CourseOfflineListener {

    @Autowired
    private SearchService searchService;

    // 监听课程下架
    @RabbitListener(bindings = @QueueBinding( // 绑定
            value = @Queue("content.queue.offline"),  // 创建队列
            exchange =@Exchange(value = "content-exchange",type = ExchangeTypes.TOPIC), // 创建交换机
            key = "course.offline" // routingKey
    ))
    public void listenCourseOffline(Long courseId){
        //数据库删除同步至es
        try {
            searchService.mysqlDeleteToEs(courseId);
            log.info("课程id：{}",courseId);
        }catch (Exception e){
            log.info("课程id：{}",courseId);
            throw new BusinessException(ErrorCode.DELETEERROR);
        }
    }
}
